package com.horoscope.astrology.zodiac.signs.free.fragment.article

import `in`.srain.cube.views.ptr.PtrDefaultHandler
import `in`.srain.cube.views.ptr.PtrFrameLayout
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.fragment.BaseFragment
import com.horoscope.astrology.zodiac.signs.free.utils.SubFragmentDecoration
import com.horoscope.astrology.zodiac.signs.free.view.CustomPtrHeader


/**
 *  Fragment 刷新Base类
 *  只有下拉刷新，上拉加载自己实现即可，配合分页加载
 * */
open class BaseSwipeRefreshFragment : BaseFragment() {

    protected var mPtrFrameLayout: PtrFrameLayout? = null // 下拉刷新 上拉加载组件
    protected var currentPage = 1
    protected lateinit var mRecyclerView: RecyclerView
    protected var header: CustomPtrHeader? = null
    protected var include:LinearLayout? = null
    protected var tvOk: TextView? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_base_swipe_refresh, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)

    }

    private fun initView(view: View) {
        mRecyclerView = view.findViewById(R.id.recycler_base_refresh)
        val layoutManager = LinearLayoutManager(activity)
        mRecyclerView.layoutManager = layoutManager
        mRecyclerView.addItemDecoration(SubFragmentDecoration(10f, 10f))
        mRecyclerView.itemAnimator?.addDuration = 500L
        mRecyclerView.itemAnimator?.removeDuration = 100L
        mRecyclerView.addOnScrollListener(RecyclerViewScrollListener())
        mPtrFrameLayout = view.findViewById(R.id.ultra_ptr_frame)
        mPtrFrameLayout?.setPtrHandler(ptr)
        header = CustomPtrHeader(activity)
        mPtrFrameLayout?.headerView = header
        mPtrFrameLayout?.addPtrUIHandler(header)
        //ptr.onRefreshBegin(mPtrFrameLayout)
        //mPtrFrameLayout?.autoRefresh()
        mPtrFrameLayout?.postDelayed({
            mPtrFrameLayout?.autoRefresh(true)
        }, 0)

        include = view.findViewById(R.id.include_error)
        tvOk = include!!.findViewById(R.id.tv_network_ok)
        tvOk!!.visibility = View.GONE
//        tvOk.setOnClickListener {
//            mPtrFrameLayout?.postDelayed({
//                mPtrFrameLayout?.autoRefresh(true)
//            }, 0)
//            include.visibility = View.GONE
//        }
    }


    private val ptr = object : PtrDefaultHandler() {
        override fun onRefreshBegin(frame: PtrFrameLayout?) {
            requestData()
        }
    }

    open fun requestData() {

    }

    open fun loadMore() {

    }

    private inner class RecyclerViewScrollListener : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                if (!recyclerView.canScrollVertically(1)) {
                    loadMore()
                }
            }
        }
    }
}